﻿namespace MvvmFx.CaliburnMicro
{
    using System;
    using System.Windows;
#if WEBGUI
    using Gizmox.WebGUI.Forms;
#else
    using System.Windows.Forms;
#endif

    /// <summary>
    /// Hosts attached properties related to view models.
    /// </summary>
    public static class View
    {
        /// <summary>
        /// The default view context.
        /// </summary>
        public static readonly object DefaultContext = new object();

        /// <summary>
        /// This defines the <see cref="P:Caliburn.Micro.View.ApplyConventions"/> attached property.
        /// </summary>
        /// <AttachedPropertyComments>
        /// <summary>A dependency property which allows the override of convention application behavior.</summary>
        /// </AttachedPropertyComments>
        public static readonly DependencyProperty ApplyConventionsProperty =
            DependencyProperty.RegisterAttached(
                "ApplyConventions",
                typeof (bool?),
                typeof (View),
                null
                );


        /*/// <summary>
        /// Executes the handler immediately if the element is loaded, otherwise wires it to the Loaded event.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="handler">The handler.</param>
        /// <returns>true if the handler was executed immediately; false otherwise</returns>
        public static bool ExecuteOnLoad(Control element, EventHandler handler)
        {
            if (element.IsHandleCreated)
            {
                handler(element, new EventArgs());
                return true;
            }
            
            EventHandler loaded = null;
            loaded = (s, e) =>
            {
                handler(s, e);
                element.HandleCreated -= loaded;
            };

            element.HandleCreated += loaded;
            return false;
        }*/

        /// <summary>
        /// Used to retrieve the root, non-framework-created view.
        /// </summary>
        /// <returns>The root element that was not created by the framework.</returns>
        /// <remarks>In certain instances the services create UI elements.
        /// For example, if you ask the window manager to show a UserControl as a dialog, it creates a window to host the UserControl in.
        /// The WindowManager marks that element as a framework-created element so that it can determine what it created vs. what was intended by the developer.
        /// Calling GetFirstNonGeneratedView allows the framework to discover what the original element was. 
        /// </remarks>
        public static Func<object, object> GetFirstNonGeneratedView = view =>
        {
            var control = view as Control;
            if (control == null)
            {
                return view;
            }

            if (control is ContentContainer)
            {
                return ((ContentContainer) control).Content;
            }

            return control;
        };

        /// <summary>
        /// Gets the convention application behavior.
        /// </summary>
        /// <param name="d">The element the property is attached to.</param>
        /// <returns>Whether or not to apply conventions.</returns>
        public static bool? GetApplyConventions(DependencyObject d)
        {
            return (bool?) d.GetValue(ApplyConventionsProperty);
        }

        /// <summary>
        /// Sets the convention application behavior.
        /// </summary>
        /// <param name="d">The element to attach the property to.</param>
        /// <param name="value">Whether or not to apply conventions.</param>
        public static void SetApplyConventions(DependencyObject d, bool? value)
        {
            d.SetValue(ApplyConventionsProperty, value);
        }
    }
}